//换一个验证码,参数是 图片控件的ID
function chgKap(imgId, fieldId){
	//如果ID为空，则默认为kaptcha
	if(imgId == null){
		imgId = 'kaptcha';
	}
	//获取新的验证码
	document.getElementById(imgId).src="Kaptcha.jpg?tmp=" + Math.random();
	
	if(fieldId == null){
		fieldId = 'kapField';
	}
	//清空输入验证码的文本框
	$("#" + fieldId).val("").focus();
	
}

//倒计时-秒
//在引用的页面中，需要使用 window.setInterval("f_djs_sec();", 1000); 来设置计时器
function f_djs_sec(spanId, urlStr){
 var s = document.getElementById(spanId);
 if(s.innerHTML == 0){
  window.location.href = urlStr;
  return false;
 }
 s.innerHTML = s.innerHTML * 1 - 1;
}


 
/**
 * 计算字符个数的JS代码
 * 1个汉字算一个字，2个字母算一个字
 * 使用办法：调用 wordsLength(‘fsewdd发到’, 5) //true
 * @param str
 * @param n
 * @return
 */	
function f_wordsLength(str, maxLength, minLength){
	var r = /[^\x00-\xff]/g;
	if(minLength == null){
		minLength = 0;
	}
	if((str.replace(r,"mm").length >= 2*minLength) && (str.replace(r,"mm").length <= 2*maxLength)){
		return true;
	}else{
		return false;
	}
}

//正则表达式匹配方案-全集
function f_regexMatches(str, type){
	//首尾不能为空格，不能含特殊字符
	if(type == 1){
		var r = /^[^*~!#$%^&()=+\\|[\]{};':",?/<>\s]?[^*~!#$%^&()=+\\|[\]{};':",?/<>]*[^*~!#$%^&()=+\\|[\]{};':",?/<>\s]?$/;
		if(r.test(str)){
			return true;
		}else{
			return false;
		}
	}
}


//用于文本框或文本域中的提示，当失去焦点且内容为空时，则复原初始的提示
//cId - 控件ID 
//tipMsg - 提示的内容
//tipColor - 提示的文字颜色。可选
function textInitTip(cId, tipMsg, tipColor, textColor){
	if ($("#" + cId).val() != null && $("#" + cId).val() != "") {
		return;
	}
	isNew = true;
	if(tipColor != null && tipColor != ""){
		$("#" + cId).css('color',tipColor);
	}else{
		$("#" + cId).css('color','#888888');
	}
	$("#" + cId).attr('value',tipMsg);
	
	$("#" + cId).focus(function(){
		if(isNew == true){
			$("#" + cId).attr('value','');
			if(textColor != null && textColor != ""){
				$("#" + cId).css('color',textColor);
			}else{
				$("#" + cId).css('color','black');
			}
			isNew = false;
		}
	});
	$("#" + cId).blur(function(){
		if(isNew == false && $.trim($("#" + cId).attr("value")) == ""){
			if(tipColor != null && tipColor != ""){
				$("#" + cId).css('color',tipColor);
			}else{
				$("#" + cId).css('color','#888888');
			}
			$("#" + cId).attr('value',tipMsg);
			isNew = true;
		}
	});
	$("#" + cId).blur();
}

//延期删除某元素的统一调用函数
//用法：在onready函数里直接调用该方法。
//setTimeout 是只执行一次
function f_setIntervalDelElement(){
	setInterval(f_delAfter8s, 8000);
}

//8秒钟后删除class属性为“delAfter5s”的元素
function f_delAfter8s(){
  if($(".delAfter8s").text() != ""){
  	$(".delAfter8s").text("");
  }
}


//等比例压缩图片的JS
//参数(图片,允许的宽度,允许的高度) 
//<img src=”/images/NullPic.gif” alt=”自动缩放后的效果” width=”200″ height=”200″ onload=”javascript:DrawImage(this,100,100)” />
function DrawImage(ImgD,iwidth,iheight){    
    var image=new Image();    
    image.src=ImgD.src;    
    if(image.width>0 && image.height>0){    
      if(image.width/image.height>= iwidth/iheight){    
          if(image.width>iwidth){      
              ImgD.width=iwidth;    
              ImgD.height=(image.height*iwidth)/image.width;    
          }else{    
              ImgD.width=image.width;      
              ImgD.height=image.height;    
          }    
      }else{    
          if(image.height>iheight){      
              ImgD.height=iheight;    
              ImgD.width=(image.width*iheight)/image.height;            
          }else{    
              ImgD.width=image.width;      
              ImgD.height=image.height;    
          }    
      }    
    }    
}   

//textId 要复制的控件的ID
//tipId 提示复制成功的ID
function f_copyToClipboard(textId, tipId){
	var text = $("#" + textId).val();
	if(window.clipboardData){
		var ok=window.clipboardData.setData("Text",text);
	if(ok){
		$("#" + tipId).text("√复制成功");
	} 
	}else{
		alert("对不起，当前浏览器不支持该操作，请直接手动复制");
	}
}

//text 要复制的内容
//tipId 提示复制成功的ID
function f_copyTextToClipboard(text, tipId){
	if(window.clipboardData){
		var ok=window.clipboardData.setData("Text",text);
		if(ok){
			$("#" + tipId).text("√复制成功");
		} 
	}else{
		alert("对不起，当前浏览器不支持该操作，请直接手动复制");
	}
}

//js暂停函数   
/**用法
 *  alert("1");   
 	Pause(this,3000);    
 	this.NextStep=function(){    
  		alert("2");   
 	} 
 */
function pause(obj,iMinSecond){    
   if (window.eventList==null) window.eventList=new Array();    
   var ind=-1;    
   for (var i=0;i<window.eventList.length;i++){    
       if (window.eventList[i]==null) {    
         window.eventList[i]=obj;    
         ind=i;    
         break;    
        }    
    }    
   if (ind==-1){    
   ind=window.eventList.length;    
   window.eventList[ind]=obj;    
   }    
  setTimeout("GoOn(" + ind + ")",iMinSecond);    
}    
  
//js继续函数   
function GoOn(ind){    
  var obj=window.eventList[ind];    
  window.eventList[ind]=null;    
  if (obj.NextStep) obj.NextStep();    
  else obj();    
} 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var showLength = 0;
var div;
var type;
var str;

function showOneByOneMain(myDiv, myShowInterval, myType) {
	div = myDiv;
	type = myType;
	str = $("#" + div).text();
	setInterval(showOneByOne, myShowInterval);
}

function showOneByOne() {
	var length = str.length;
	if (type == 0) {
			$("#" + div).text(str.substr(0,showLength));
			showLength = showLength + 1;
			if (showLength > length) {
				showLength = 0;
			}
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





//拼写SQL ,添加了like前面的%
function getWherePart2(fieldName, controlName, strOperate, fieldType)
{
	var strWherePart = "";
	var value = "";
	if(controlName == "" || controlName == null) controlName = fieldName;
	value = eval("fm." + trim(controlName) + ".value");
	if(value == "" || value == null) return strWherePart;
	if(fieldType == null || fieldType == "") fieldType = "0";
	if(strOperate == "" || strOperate == null) strOperate = "=";
	if(fieldType == "0")
	{
		// 0:字符型
		if(strOperate == "like")
		{
			strWherePart = " and " + trim( fieldName ) + " like '%25" + trim( value ) + "%25'";
		}
		else
		{
			strWherePart = " and " + trim( fieldName ) + trim( strOperate ) + "'" + trim( value ) + "' ";
		}
	}
	if(fieldType == "1")
	{
		// 1:数字型
		strWherePart = " and " + trim(fieldName) + trim(strOperate) + trim(value) + " ";
	}
	return strWherePart;
}


//获取被选中的radioBox的值
function getCheckedRadioBoxValue(radioName)  
{  
  var obj = document.getElementsByName(radioName);
  for(i = 0; i < obj.length; i++)
  {  
	  if (obj[i].checked == true) {
	  	return obj[i].value;
	  }
  }
  return null;
}


//显示已经逝去的时间的方法
//startDate 相对的时间 spanId 显示的SPAN showLevel显示方式
function showPastTime(startDate,spanId,showLevel){

	 if (showLevel != 0 && showLevel != 1) {
	 	showLevel = 1;
	 }
	 
	 var tMillisecond = 1;
	 var tSecond = tMillisecond * 1000;
	 var tMinute = tSecond * 60;
	 var tHour = tMinute * 60;
	 var tDay = tHour * 24;
	 
	 var startDate= new Date(startDate);
	 var endDate= new Date();
	 
	 var allMillisecond = Math.round((endDate.getTime()-startDate.getTime()));
	 
	 if (allMillisecond > 0){
	 var day = Math.floor(allMillisecond/tDay);
	 if(day > 0){
	  allMillisecond = allMillisecond - tDay * day;
	 } else {
	  day = 0;
	 }
	 var hour = Math.floor(allMillisecond/tHour);
	 if(hour > 0){
	  allMillisecond = allMillisecond - tHour * hour;
	 } else {
	  hour = 0;
	 }
	 
	 var minute = Math.floor(allMillisecond/tMinute);
	 if(minute > 0){
	  allMillisecond = allMillisecond - tMinute * minute;
	 } else {
	  minute = 0;
	 }
	 
	 var second = Math.floor(allMillisecond/tSecond);
	 if(second > 0){
	  allMillisecond = allMillisecond - tSecond * second;
	 } else {
	  second = 0;
	 }
	 
	 var millisecond = allMillisecond;

	 if (showLevel == 0) {
	  var str = day + "天" + hour + "小时" + minute + "分" + second + "秒" + millisecond + "毫秒";
	 } else if (showLevel == 1) {
	  var str = day + "天" + hour + "小时" + minute + "分" + second + "秒";
	 }
	  document.getElementById(spanId).innerHTML=str; 
}

}



//倒计时 laterTime-未来的时间 showLevel -显示格式
function timeDJS(laterTime, showLevel) {

	 var tMillisecond = 1;
	 var tSecond = tMillisecond * 1000;
	 var tMinute = tSecond * 60;
	 var tHour = tMinute * 60;
	 var tDay = tHour * 24;
	 
	 var laterTime = new Date(laterTime);
	 var nowTime   = new Date();
	 
	 var allMillisecond = Math.round((laterTime.getTime() - nowTime.getTime()));
	 
	 
	 if (allMillisecond > 0){
		 var day = Math.floor(allMillisecond/tDay);
		 if(day > 0){
		  allMillisecond = allMillisecond - tDay * day;
		 } else {
		  day = 0;
		 }
		 var hour = Math.floor(allMillisecond/tHour);
		 if(hour > 0){
		  allMillisecond = allMillisecond - tHour * hour;
		 } else {
		  hour = 0;
		 }
		 
		 var minute = Math.floor(allMillisecond/tMinute);
		 if(minute > 0){
		  allMillisecond = allMillisecond - tMinute * minute;
		 } else {
		  minute = 0;
		 }
		 
		 var second = Math.floor(allMillisecond/tSecond);
		 if(second > 0){
		  allMillisecond = allMillisecond - tSecond * second;
		 } else {
		  second = 0;
		 }
		 
		 var millisecond = allMillisecond;
	
		 if (showLevel == 0) {
		  var str = day + "天" + hour + "小时" + minute + "分" + second + "秒" + millisecond + "毫秒";
		 } else if (showLevel == 1) {
		  var str = day + "天" + hour + "小时" + minute + "分" + second + "秒";
		 }
		 return str; 
	 } else {
	 	 if (showLevel == 0) {
		  return "0天0小时0分0秒0毫秒";
		 } else if (showLevel == 1) {
		 	 return "0天0小时0分0秒"; 
		 }
	 }
}


//倒计时 laterTime-未来的时间 showLevel -显示格式
function timeShowDJS(laterTime, spanId, showLevel) {
	 var tMillisecond = 1;
	 var tSecond = tMillisecond * 1000;
	 var tMinute = tSecond * 60;
	 var tHour = tMinute * 60;
	 var tDay = tHour * 24;
	 
	 var laterTime = new Date(laterTime);
	 var nowTime   = new Date();
	 
	 var allMillisecond = Math.round((laterTime.getTime() - nowTime.getTime()));
	 
	 
	 if (allMillisecond > 0){
		 var day = Math.floor(allMillisecond/tDay);
		 if(day > 0){
		  allMillisecond = allMillisecond - tDay * day;
		 } else {
		  day = 0;
		 }
		 var hour = Math.floor(allMillisecond/tHour);
		 if(hour > 0){
		  allMillisecond = allMillisecond - tHour * hour;
		 } else {
		  hour = 0;
		 }
		 
		 var minute = Math.floor(allMillisecond/tMinute);
		 if(minute > 0){
		  allMillisecond = allMillisecond - tMinute * minute;
		 } else {
		  minute = 0;
		 }
		 
		 var second = Math.floor(allMillisecond/tSecond);
		 if(second > 0){
		  allMillisecond = allMillisecond - tSecond * second;
		 } else {
		  second = 0;
		 }
		 
		 var millisecond = allMillisecond;
	
		 if (showLevel == 0) {
		  var str = day + "天" + hour + "小时" + minute + "分" + second + "秒" + millisecond + "毫秒";
		  $("#" + spanId).text(str);
		 } else if (showLevel == 1) {
		  var str = day + "天" + hour + "小时" + minute + "分" + second + "秒";
		  $("#" + spanId).text(str);
		 }
		 setTimeout("timeShowDJS('" + laterTime  + "', '" + spanId + "', " + showLevel + ")",1000);
		 
		 return str; 
	 } else {
	 	 if (showLevel == 0) {
		  return "0天0小时0分0秒0毫秒";
		 } else if (showLevel == 1) {
		 	 return "0天0小时0分0秒"; 
		 }
	 }
}


//~~~~~~~~~~~~~~~~~~~倒计时，精确模式，从服务器获取对比时间~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*用法：
timeShowDJS_ServerTime("2010/12/12 00:00:00", "daoJiShiSpan", 1, 1000, "anzhenhome");
laterTime - 未来的时间
spanId - 显示倒计时的span
showLevel - 显示级别 0 毫秒 1 秒
showInterval - 显示间隔，刷新的间隔
contextPath - 项目的名字
*/
var serverTime = "";
var differTime = 0;

var startGetTime = null;
var endGetTime = null;
var netDelayed = 0;


//倒计时 laterTime-未来的时间 showLevel -显示格式
function timeShowDJS_ServerTime(laterTime, spanId, showLevel, showInterval, contextPath) {
	 var str;
	 if (serverTime == "") {
		 startGetTime = new Date();
		$.get("/" + contextPath + "/common/getSystemTime.do?rnd=" + Math.random() ,null, function(data){
			endGetTime = new Date();
			netDelayed = Math.round((endGetTime.getTime() - startGetTime.getTime()));
			serverTime = data;  
			differTime = Math.round((new Date(praseDateStr(serverTime))).getTime() - (new Date()).getTime());
		 });
		
		 if (showLevel == 0) {
			 str = "-天-时-分-秒-毫秒";
		 } else if (showLevel == 1) {
			 str = "-天-时-分-秒";
		 }
		 
	 } else {
		 
			 var tMillisecond = 1;
			 var tSecond = tMillisecond * 1000;
			 var tMinute = tSecond * 60;
			 var tHour = tMinute * 60;
			 var tDay = tHour * 24;
			 var laterTime = new Date(laterTime);
			 var nowTime   = new Date();
			 var allMillisecond = Math.round((laterTime.getTime() - nowTime.getTime())) - differTime + netDelayed;
			 
			 if (allMillisecond > 0){
				 var day = Math.floor(allMillisecond/tDay);
				 if(day > 0){
					 allMillisecond = allMillisecond - tDay * day;
				 } else {
					 day = 0;
				 }
				 var hour = Math.floor(allMillisecond/tHour);
				 if(hour > 0){
					 allMillisecond = allMillisecond - tHour * hour;
				 } else {
					 hour = 0;
				 }
				 
				 var minute = Math.floor(allMillisecond/tMinute);
				 if(minute > 0){
					 allMillisecond = allMillisecond - tMinute * minute;
				 } else {
					 minute = 0;
				 }
				 
				 var second = Math.floor(allMillisecond/tSecond);
				 if(second > 0){
					 allMillisecond = allMillisecond - tSecond * second;
				 } else {
					 second = 0;
				 }
				 
				 var millisecond = allMillisecond;
				  
				 
				 day = day < 10 ? "0" + day : day;
				 hour = hour < 10 ? "0" + hour : hour;
				 minute = minute < 10 ? "0" + minute : minute;
				 second = second < 10 ? "0" + second : second;
				 millisecond = millisecond < 10 ? "0" + millisecond : millisecond;
				 
				 if (serverTime == "") {
					 day = "-";
					 hour = "-";
					 minute = "-";
					 second = "-";
					 millisecond = "-";
				 }
				 
				 
				 if (showLevel == 0) {
					 str = day + "天" + hour + "时" + minute + "分" + second + "秒" + millisecond + "毫秒";
				 } else if (showLevel == 1) {
					 str = day + "天" + hour + "时" + minute + "分" + second + "秒";
				 }
			 } else {
				 if (showLevel == 0) {
					 return "0天0时0分0秒0毫秒";
				 } else if (showLevel == 1) {
					 return "0天0时0分0秒"; 
				 }
			 }
	 }
	 
	 $("#" + spanId).text(str);
	 setTimeout("timeShowDJS_ServerTime('" + laterTime  + "', '" + spanId + "', " + showLevel + ",'" + contextPath + "')",showInterval);
	 
 
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



//将固定格式的日期2010-09-15 14:00:00 格式化成为 2010/09/15 14:00:00
//考虑到该方法为监控平台使用，需要效率，所以有点硬编码，不推荐为公用组件。
function praseDateStr(oldDateStr) {
	var year = oldDateStr.substr(0,4);
	var month = oldDateStr.substr(5,2);
	var day = oldDateStr.substr(8,2);
	var other = oldDateStr.substr(10);
	return year + "/" + month + "/" + day + other;
}



//对JS中endWith的扩展，判断某个字符串是否以某个字符串结尾
String.prototype.endWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
   return false;
if(this.substring(this.length-str.length)==str)
   return true;
else
   return false;
return true;
}

//对JS中startWith的扩展，判断某个字符串是否以某个字符串开始
//用法：var value = "abcdefg"; value.startWith("abc"); //return true
String.prototype.startWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
 return false;
if(this.substr(0,str.length)==str)
   return true;
else
   return false;
return true;
}


/**  
* Make a map like java.  
* You can use this map like this :   
* var myMap = new Map();  
* myMap.put("key","value");  
* var key = myMap.get("key");  
*/  
function Map() {   

  this.elements = new Array();   

  this.size = function() {   
      return this.elements.length;   
  }   

  this.isEmpty = function() {   
      return (this.elements.length < 1);   
  }   

  this.clear = function() {   
      this.elements = new Array();   
  }   

  this.put = function(_key, _value) {   
      this.elements.push({key:_key, value:_value});   
  }   

  this.remove = function(_key) {   
      var bln = false;   

      try {   
          for (i = 0; i < this.elements.length; i++) {   
              if (this.elements[i].key == _key) {   
                  this.elements.splice(i, 1);   
                  return true;   
              }   
          }   
      } catch(e) {   
          bln = false;   
      }   
      return bln;   
  }   

  this.get = function(_key) {   
      try{    
          for (i = 0; i < this.elements.length; i++) {   
              if (this.elements[i].key == _key) {   
                  return this.elements[i].value;   
              }   
          }   
      }catch(e) {   
          return null;   
      }   
  }   

  this.element = function(_index) {   
      if (_index < 0 || _index >= this.elements.length) {   
          return null;   
      }   
      return this.elements[_index];   
  }   

  this.containsKey = function(_key) {   
      var bln = false;   
      try {   
          for (i = 0; i < this.elements.length; i++) {   
              if (this.elements[i].key == _key) {   
                  bln = true;   
              }   
          }   
      }catch(e) {   
          bln = false;   
      }   
      return bln;   
  }   

  this.containsValue = function(_value) {   
      var bln = false;   
      try {   
          for (i = 0; i < this.elements.length; i++) {   
              if (this.elements[i].value == _value){   
                  bln = true;   
              }   
          }   
      } catch(e) {   
          bln = false;   
      }   
      return bln;   
  }   

  this.values = function() {   
      var arr = new Array();   
      for (i = 0; i < this.elements.length; i++) {   
          arr.push(this.elements[i].value);   
      }   
      return arr;   
  }   

  this.keys = function() {   
      var arr = new Array();   
      for (i = 0; i < this.elements.length; i++) {   
          arr.push(this.elements[i].key);   
      }   
      return arr;   
  }   
}   

//获取服务器当前系统时间
function getServerCurrentTime (contextPath) {
	$.get("/" + contextPath + "/common/getSystemTime.do?rnd=" + Math.random() ,null, function(data){
		return data;
	 });
}
 



 
